package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* ClipView07
* */
class ClipView07 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_F3236D)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            //style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.teal_200)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    private val mPath2 by lazy {
        Path()
    }

    private val mPath3 by lazy {
        Path()
    }

    private val mPath4 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //setBackgroundColor(context.resources.getColor(R.color.color_FDBB8FFF))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            save()
            mPath3.apply {
                addRoundRect(
                    -cx,
                    -cy,
                    cx,
                    cy,
                    cx / 5,
                    cy / 5,
                    Path.Direction.CW
                )
            }

            mPath4.apply {
                addRect(
                    -cx,
                    -cy,
                    cx,
                    cy,
                    Path.Direction.CW
                )
                clipPath(mPath3)
            }
            drawPath(mPath4, mPaint1)
            restore()

            mPaint.strokeWidth = (cx / 18).coerceAtLeast(context.resources.getDimension(R.dimen.dp_1))
            mPath0.apply {
                moveTo(-cx * 2 / 5, -cy * 2 / 5)
                lineTo(cx * 2 / 5, cy * 2 / 5)

                moveTo(cx * 2 / 5, -cy * 2 / 5)
                lineTo(-cx * 2 / 5, cy * 2 / 5)
            }

            mPath1.apply {
                addArc(
                    -cx * 6 / 7,
                    -cy * 6 / 7,
                    cx * 6 / 7,
                    cx * 6 / 7,
                    0F,
                    360F
                )
            }

            save()
            mPath2.apply {
                //Path.FillType.WINDING---去除两者之间的公共部分，两者之间的非公共部分保留原样，两者之间方向互异;方向相同则不做处理
                //Path.FillType.INVERSE_WINDING---保留两者非公共部分和公共部分，两者之间方向互异;方向相同则不做处理
                //Path.FillType.EVEN_ODD---去除两者之间的公共部分，两者之间的非公共部分保留原样，不受两者之间方向影响
                //Path.FillType.INVERSE_EVEN_ODD---保留两者非公共部分和公共部分，两者之间方向互异，不受两者之间方向影响
                fillType = Path.FillType.INVERSE_WINDING
                addRect(
                    -cx,
                    -cy,
                    cx,
                    cy,
                    Path.Direction.CW
                )
                addPath(mPath0)
                clipPath(mPath1)
            }
            drawPath(mPath2, mPaint)
            restore()

            restore()
        }
    }
}